package client.comm.baoding.ui

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.lifecycle.Observer
import client.comm.commlib.toast.toast
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityRegisiterBinding
import client.comm.baoding.ui.vm.RegisterViewModel
import java.util.*

class RegisterActivity : BaseMiddleActivity<RegisterViewModel, ActivityRegisiterBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_regisiter
    }

    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun initViewModel(): RegisterViewModel = createViewModel()

    override fun onDestroy() {
        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }
        super.onDestroy()
    }

    override fun initView() {
        binding?.apply {

            login = this@RegisterActivity
            vm = viewModel

        }
    }

    override fun observerUI() {
        viewModel.registerRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("注册成功")
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
        viewModel.isShowPwdReg.observe(this, Observer {
            if (it) {
                binding!!.etPwdReg.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding!!.etPwdReg.setSelection(
                    (binding!!.etPwdReg.getText().toString().trim()).length
                );
            } else {
                binding!!.etPwdReg.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding!!.etPwdReg.setSelection(
                    (binding!!.etPwdReg.getText().toString().trim()).length
                );
            }
        })
        viewModel.isShowPwdAgainReg.observe(this, Observer {
            if (it) {
                binding!!.etPwdAgainReg.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding!!.etPwdAgainReg.setSelection(
                    (binding!!.etPwdAgainReg.getText().toString().trim()).length
                );
            } else {
                binding!!.etPwdAgainReg.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding!!.etPwdAgainReg.setSelection(
                    (binding!!.etPwdAgainReg.getText().toString().trim()).length
                );
            }
        })
        viewModel.codeRet.observe(this, Observer {

            it?.apply {

                if (isSuccess) {
                    toast("获取验证码成功")
                    timerTask?.let { it.cancel() }
                    timer?.let { it.cancel() }
                    viewModel.getCodeEnable.postValue(false)
                    timerTask = object : TimerTask() {
                        var count = 60
                        override fun run() {
                            count--;
                            viewModel.getCodeTxt.postValue("${count}秒后获取")
                            if (count <= 0) {
                                viewModel.getCodeEnable.postValue(true)
                                viewModel.getCodeTxt.postValue("获取验证码")
                                timerTask!!.cancel()
                            }
                        }
                    }
                    timer = Timer().apply { schedule(timerTask, 0, 1000) }
                } else {
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"
                    toast(it.message)
                }

            }
        })
    }

    fun lookxy() {
//        startActivity(Intent(this, WebActivity::class.java).apply {
//            putExtra("adInfo", "${HhRetroHttp.BASE_HOST}regXy")
//            putExtra("title", "用户注册协议")
//        })
    }
}